package edu.nyu.hps.assignment5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int port = 8484,player=0,stones=0,time=0,round=0;
		Voronoi game;

		// New client
		Socket client;
		InputStreamReader isr;
		BufferedReader in = null;
		PrintWriter out = null;

		boolean connected = false;
		String response = null;
		//port = Integer.parseInt(args[0]);
		try {
			client = new Socket("localhost", port);
			isr = new InputStreamReader(client.getInputStream());
			in = new BufferedReader(isr);
			out = new PrintWriter(client.getOutputStream(), true);

			while (!connected) {
				response = null;
				out.println("JOIN\nRohan\n");

				// Waiting for the ACK or NACK responses from the server
				while ((response = in.readLine()) == null) {
				}

				if (response.charAt(0) == 'A') {
					System.out.println("Socket Acknowledged!");
					player = in.readLine().equalsIgnoreCase("0") ? 0 : 1;
					stones = Integer.parseInt(in.readLine());
					//response = in.readLine();
					connected = true;
					System.out.println("I have to play with" + stones + "stones");
				}
				else{
					continue;
				}
			}	
			
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(player==0){
			game = new Red(player,stones);
		}
		else{
			game  = new Blue(player,stones);
		}
		while(true){
			try{
				response = null;
				while ((response = in.readLine()) == null) {
				}
				int totalRedStones=0,totalBlueStones = 0;
				String[] blueStones = null,redStones = null;
				if (response.indexOf("PLAY") == 0) {
					// Taking time as Input
					time = Integer.parseInt(in.readLine());
					
					//Taking stones of red as input
					response = in.readLine();
					if(round!=0 || player==1){
						redStones = response.split(",");
						totalRedStones = redStones.length;
						System.out.println("totalRedStones: " + totalRedStones);
					}
					
					//Taking area of red
					response = in.readLine();
					double redArea = Integer.parseInt(response);
					
					//Taking stones of blue as input					
					response = in.readLine();
					if(round!=0){	
						blueStones = response.split(",");
						totalBlueStones = blueStones.length;
						System.out.println("totalBlueStones: " + totalBlueStones);
					}
					
					//Taking area of blue as input
					response = in.readLine();
					double blueArea = Integer.parseInt(response);
					
					if(player==0){
						game.time = time;
						game.myArea = redArea;
						
						if(totalBlueStones!=0){
							int newX = Integer.parseInt(blueStones[totalBlueStones-2]);
							int  newY = Integer.parseInt(blueStones[totalBlueStones-1]);
							game.opponentStones.add(game.grid[newX][newY]);
							game.grid[newX][newY].taken=true;
							game.grid[newX][newY].takenBy=1;
							game.opponentArea = blueArea;
						}
					}
					else{
						game.time = time;
						game.myArea = blueArea;
						
						if(totalRedStones!=0){
							int newX = Integer.parseInt(redStones[totalRedStones-2]);
							int newY = Integer.parseInt(redStones[totalRedStones-1]);
							game.opponentStones.add(game.grid[newX][newY]);
							game.grid[newX][newY].taken=true;
							game.grid[newX][newY].takenBy=0;
							game.opponentArea = redArea;
						}
					}
					Point myMove = game.throwAt();
					game.createImage();
					System.out.println("Oponent Area: " + game.opponentArea);
					System.out.println("My Area:" + game.myMyArea );
					if(myMove==null){
						System.out.println("I dont know what to throw");
						break;
					}
					out.println("THROW\n" + myMove.x + "\n" + myMove.y + "\n");
					System.out.println("THROW: (" + myMove.x + ","+ myMove.y + ")");
					round++;
				}
				else if(response.indexOf("STATUS") == 0){
					game.updateClosestStone();
					game.createImage();
					System.out.println("Oponent Area: " + game.opponentArea);
					System.out.println("My Area:" + game.myMyArea );
					break;
				}
			}catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}
